import * as type from './mutation-type'
import User from '../api/user'
import Address from '../api/address'
import Product from '../api/product'
import Order from '../api/order'
import Lense from '../api/lense'
import Report from '../api/report'
import Coupon from '../api/coupon'
import Optometrist from '../api/optometrist'
import Article from '../api/article'

// 地址
export const getAddresses = async ({ dispatch }, value) => {
  return Address.list(value)
}
export const createAddress = async ({ dispatch }, ...value) => {
  return Address.create(...value)
}
export const deleteAddress = async ({ dispatch }, ...value) => {
  return Address.delete(...value)
}
export const updateAddress = async ({ dispatch }, ...value) => {
  return Address.update(...value)
}

// 用户
// 登录
export const logIn = async ({ dispatch }, value) => {
  dispatch(type.LOG_IN, await User.logIn(value))
}
// 登出
export const logOut = ({ dispatch }) => {
  dispatch(type.LOG_OUT)
  router.go({name: 'login'})
  // dispatch('router/ROUTE_CHANGED', {
  //   path: { name: 'login' }
  // })
}
// 列表
export const getUsers = async ({ dispatch }, value) => {
  const result = await User.list(value)
  dispatch(type.GET_USERS, result, value.page>1)
  return result
}

// 详情
export const getUser = async ({ dispatch }, value) => {
  const result = await User.get(value)
  dispatch(type.GET_USER, result)
  return result
}

// 搜索
export const searchUser = async ({ dispatch }, value) => {
  const result = await User.search(value)
  dispatch(type.SEARCH_USER, result)
  return result
}


// 产品
// 列表
export const getProducts = async ({ dispatch }, value) => {
  dispatch(type.GET_PRODUCTS, await Product.list(value))
}

// 详情
export const getProduct = async ({ dispatch }, value) => {
  const result = await Product.get(value)
  dispatch(type.GET_PRODUCT, result)
  return result
}

// 添加
export const createProduct = async ({ dispatch }, value) => {
  const { id } = await Product.create(value)
  router.go({ name: 'product', params: { id }, query: { mode: 'edit' } })
}

// 修改
export const updateProduct = async ({ dispatch }, value) => {
  await Product.update(value)
}

// 搜索
export const searchProduct = async ({ dispatch }, value) => {
  const result = await Product.search(value)
  dispatch(type.SEARCH_PRODUCT, result)
  return result
}


// 订单
// 列表
export const getOrders = async ({ dispatch }, value) => {
  const result = await Order.list(value)
  dispatch(type.GET_ORDERS, result, value.page>1)
  return result
}

// 详情
export const getOrder = async ({ dispatch }, value) => {
  dispatch(type.GET_ORDER, await Order.get(value))
}

// 添加
export const createOrder = async ({ dispatch }, value) => {
  return await Order.create(value)
  // const { id } = await Order.create(value)
  // router.go({ name: 'order', params: { id }, query: { mode: 'edit' } })
}

// 修改
export const updateOrder = async ({ dispatch }, value) => {
  await Order.update(value)
}


// 镜片
// 列表
export const getLenses = async ({ dispatch }, value) => {
  const result = await Lense.list(value)
  dispatch(type.GET_LENSES, result)
  return result
}

// 验光师
// 列表
export const getOptometrists = async ({ dispatch }, value) => {
  dispatch(type.GET_OPTOMETRISTS, await Optometrist.list(value))
}

// 详情
export const getOptometrist = async ({ dispatch }, value) => {
  const result = await Optometrist.get(value)
  dispatch(type.GET_OPTOMETRIST, result)
  return result
}

// 添加
export const createOptometrist = async ({ dispatch }, value) => {
  const { id } = await Optometrist.create(value)
  router.go({ name: 'optometrist', params: { id }, query: { mode: 'edit' } })
}

// 修改
export const updateOptometrist = async ({ dispatch }, value) => {
  await Optometrist.update(value)
}


// 验光报告
// 列表
export const getReports = async ({ dispatch }, value) => {
  dispatch(type.GET_REPORTS, await Report.list(value))
}

// 详情
export const getReport = async ({ dispatch }, value) => {
  const result = await Report.get(value)
  dispatch(type.GET_REPORT, result)
  return result
}

// 添加
export const createReport = async ({ dispatch }, value) => {
  const { id } = await Report.create(value)
  router.go({ name: 'report', params: { id }, query: { mode: 'edit' } })
}

// 修改
export const updateReport = async ({ dispatch }, value) => {
  await Report.update(value)
}


// 优惠券
// 列表
export const getCoupons = async ({ dispatch }, value) => {
  dispatch(type.GET_COUPONS, await Coupon.list(value))
}

// 添加
export const createCoupon = async ({ dispatch }, value) => {
  await Coupon.create(value)
  // router.go({ name: 'coupon', params: { id }, query: { mode: 'edit' } })
}

// 修改
export const updateCoupon = async ({ dispatch }, value) => {
  await Coupon.update(value)
}

// 给用户
export const toUserCoupon = async ({ dispatch }, value) => {
  await Coupon.toUser(value)
}

// 批量列表
export const getallCoupons = async ({ dispatch }, value) => {
  dispatch(type.GET_COUPONS, await Coupon.list_all(value))
}

// 添加
export const newCoupon = async ({ dispatch }, value) => {
  await Coupon.new(value)
  // router.go({ name: 'coupon', params: { id }, query: { mode: 'edit' } })
}

// 批量列表
export const getchannelCoupons = async ({ dispatch }, value) => {
  dispatch(type.GET_COUPONS, await Coupon.list_channel(value))
}

// 批量删除列表
export const deletechannelCoupons = async ({ dispatch }, value) => {
  dispatch(type.GET_COUPONS, await Coupon.delete_channel(value))
}



// Word文章
// 列表
export const getArticles = async ({ dispatch }, value) => {
  dispatch(type.GET_ARTICLES, await Article.list(value))
}

// 添加
export const createArticle = async ({ dispatch }, value) => {
  await Article.create(value)
  // router.go({ name: 'coupon', params: { id }, query: { mode: 'edit' } })
}

// 修改
export const updateArticle = async ({ dispatch }, value) => {
  await Article.update(value)
}

//查看 
export const showArticle = async ({ dispatch }, value) => {
  dispatch(type.GET_ARTICLES, await Article.show(value))
}

//删除 
export const deleteArticle = async ({ dispatch }, value) => {
  dispatch(type.GET_ARTICLES, await Article.delete(value))
}


